
/**
 * https://apifox.com/apidoc/shared-3c75896c-e80b-431d-b482-c28604cc7df6
 */

let NetWrok = function() {
	console.log("实例NetWrok");
	this.webConfig={};
	this.heartTime=-1;
	this.socketurl='';
	this.websocket=null;
};


NetWrok.prototype.init=function(webData,socketurl){
	if(webData!=null)this.webConfig=webData;
	if(socketurl!=null)this.socketurl=socketurl;
	console.log("NetWrok init",this.webConfig,this.socketurl);
	this.websocket = new WebSocket(this.socketurl);
	this.websocket.onopen =this.onOpen.bind(this);
	this.websocket.onclose =this.onClose.bind(this);
	this.websocket.onmessage = this.onmessage.bind(this);
	this.websocket.onerror = this.onError.bind(this);
}

NetWrok.prototype.onOpen=function(evt){
	console.log("NetWrok onOpen 开始登录");
	//登录
	let params={};
	for(let key in this.webConfig){
		params[key]=this.webConfig[key];
	}

	let vo={"cmd": "Login", "params": params};
	this.send(vo);
}
NetWrok.prototype.reLink=function(){

	this.heartTime=-1;
	this.websocket.onopen =null;
	this.websocket.onclose =null;
	this.websocket.onmessage = null;
	this.websocket.onerror = null;
	this.websocket=null;
	this.init();

}
NetWrok.prototype.onClose=function(evt){
	console.log("NetWrok onClose");
	winApp.sendNotification(MEventCMD.VIEW_SYSLabelNoticeView, "服务器连接已断开!");
	if(this.heartTime!=-1){
		clearInterval(this.heartTime);
		this.heartTime=-1;
	}

}
NetWrok.prototype.doClose=function(){
	this.websocket.close()
}

NetWrok.prototype.onmessage=function(evt){
	console.log("NetWrok onmessage",evt.data);
	let data=JSON.parse(evt.data);
	this.onExtensionResponse(data);
}
NetWrok.prototype.onError=function(evt){
	console.log("NetWrok onError");
}

NetWrok.prototype.send=function(data){
	// console.log("NetWrok send",data);
	this.websocket.send(JSON.stringify(data));
}
NetWrok.prototype.sendHear=function(){
	console.log("NetWrok sendHear 心跳");
	let vo={"cmd": "Heart", "params": {}};
	this.send(vo);
}
/**自定义命令**/
NetWrok.prototype.onExtensionResponse = function(evtParams) {

	if(evtParams.status==-1 && evtParams.cmd!='DrawResult'){
		winApp.sendNotification(MEventCMD.VIEW_LabelNoticeView, evtParams.errorMsg);
		return;
	}
	winApp.sendNotification(MEventCMD.SRS_DATA_handle, evtParams);
	if(evtParams.cmd==='Index'){
		if(this.heartTime==-1){
			this.heartTime=setInterval(this.sendHear.bind(this),60*1000);
		}
	}

}

NetWrok.prototype.sendLoginOut=function(){
	console.log("NetWrok sendLoginOut 退出游戏");
	let vo={"cmd": "LoginOut", "params": {}};
	this.send(vo);
}
/**
 *
 * @param num 砸蛋次数 1=1次；10=10次
 */
NetWrok.prototype.sendDraw=function(num,gameType){
	console.log("NetWrok sendDraw ==砸蛋抽奖",num);

	let vo={"cmd": "Draw", "params": {'num':num,'gameType':gameType}};
	this.send(vo);
}
/**
 * 说明申请
 * size:1长屏
 */
NetWrok.prototype.sendBanner=function(size){
	console.log("NetWrok sendBanner",size);
	if(size==null)size=0;
	console.log("NetWrok sendBanner 说明申请");
	let vo={"cmd": "Banner", "params": {'size':size}};
	this.send(vo);
}
/**
 * 任务领取奖励
 */
NetWrok.prototype.sendGetAward=function(taskId){
	console.log("NetWrok sendGetAward 任务领取奖励");
	let vo={"cmd": "GetAward", "params": {"taskId":taskId}};
	this.send(vo);
}
/**
 * 申请每日任务列表
 */
NetWrok.prototype.sendTaskList=function(){
	console.log("NetWrok TaskList 每日任务列表");
	let vo={"cmd": "TaskList", "params": {}};
	this.send(vo);
}
/**
* 积分商城申请
*/
NetWrok.prototype.sendShop=function(){
	console.log("NetWrok sendShop 积分商城");
	let vo={"cmd": "ScoreShop", "params": {}};
	this.send(vo);
}
/**
 * 积分商城申请 根据标签ID
 */
NetWrok.prototype.sendScoreGoodsByCate=function(cateId){
	console.log("NetWrok sendScoreGoodsByCate 根据标签ID积分商城",);
	let vo={"cmd": "ScoreGoodsByCate", "params": {cateId:cateId}};
	this.send(vo);
}
/**
 * 商店兑换
 *cateId 标签ID
 * amount 兑换数量
 * goodsId 商品ID
 */
NetWrok.prototype.sendExchange=function(cateId,amount,goodsId){
	console.log("NetWrok sendExchange 碎片商店兑换申请",cateId,amount,goodsId);
	let vo={"cmd": "ExcScoreGoods", "params": {'cateId':cateId,'amount':amount,'goodsId':goodsId}};
	this.send(vo);
}

/**获取排行榜游戏 1=个人富豪榜 2=家族财运榜**/
NetWrok.prototype.getRank=function(type){
	console.log("NetWrok getRank 获取排行榜游戏",type);
	let vo={"cmd": "RankList", "params": {'type':type}};
	this.send(vo);
}
/**排行榜 隐藏功能 hideStatus 0=显示;1=隐藏**/
NetWrok.prototype.RankHide=function(hideStatus){
	console.log("NetWrok RankHide 排行榜 隐藏功能");
	let vo={"cmd": "RankHide", "params": {'hideStatus':hideStatus}};
	this.send(vo);
}
export default NetWrok;
